import {ref} from 'vue'
import {defineStore} from 'pinia'

export const useOrderStore = defineStore('order', () => {
    const orderInfo = ref([
        {
            id: 0,
            order: "1001",//订单号码
            time: "2022-05-05",//下单时间
            money: "￥100",//订单金额
            status: "未下单",//订单状态
            name: "小明",//用户名
            phone: "123456789",//手机号
            address: "北京市朝阳区"//地址
        },
    ])
//增加
    const increment = (item) => {
        orderInfo.value.push(item)
        sessionStorage.setItem('orderInfo', JSON.stringify(orderInfo.value))
    }
    //编辑
    const edit = (item) => {
        orderInfo.value.map((v, i) => {
            if (v.id === item.id) {
                orderInfo.value[i] = item
            }
        })
        sessionStorage.setItem('orderInfo', JSON.stringify(orderInfo.value))
    }
//删除
    const del = (id) => {
        orderInfo.value.map((v, i) => {
            if (v.id === id) {
                orderInfo.value.splice(i, 1)
            }
        })
        sessionStorage.setItem('orderInfo', JSON.stringify(orderInfo.value))
    }


    return {orderInfo, increment, edit, del}
})
